package com.kawa.kawa_ad;

import android.app.Activity;
import android.content.res.AssetManager;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.anythink.core.api.ATSDK;
import com.kawa.kawa_ad.handler.IntersHandler;
import com.kawa.kawa_ad.handler.RewardHandler;
import com.kawa.kawa_ad.handler.SplashHandler;

import java.lang.ref.WeakReference;

import io.flutter.app.FlutterApplication;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** KawaAdPlugin */
public class KawaAdPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  private FlutterApplication mApp;
  private static WeakReference<Activity> mActivity;
  private BinaryMessenger mBinaryMessenger;
  private MethodChannel mMethodChannel;
  static public String userId;
  private ViewGroup contentView;
  private AssetManager assetManager;
  private FlutterAssets flutterAssets;

  private RewardHandler mRewardHandler;
  private SplashHandler mSplashHandler;
  private IntersHandler mIntersHandler;

  public static Activity getActivity(){
    return mActivity.get();
  }

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
    mApp = (FlutterApplication) binding.getApplicationContext();
    mBinaryMessenger = binding.getBinaryMessenger();
    mMethodChannel = new MethodChannel(mBinaryMessenger, "com.mob.adsdk/method");
    mMethodChannel.setMethodCallHandler(this);
    assetManager = binding.getApplicationContext().getAssets();
    flutterAssets = binding.getFlutterAssets();
//    binding.getPlatformViewRegistry().registerViewFactory("com.mob.adsdk/banner", new BannerAdViewFactory(mBinaryMessenger));
//    binding.getPlatformViewRegistry().registerViewFactory("com.mob.adsdk/native", new NativeAdViewFactory(mBinaryMessenger));
  }

  public static void registerWith(Registrar registrar) {
    KawaAdPlugin plugin = new KawaAdPlugin();
    plugin.mApp = (FlutterApplication) registrar.context().getApplicationContext();
    plugin.mBinaryMessenger = registrar.messenger();
    plugin.mMethodChannel = new MethodChannel(registrar.messenger(), "com.mob.adsdk/method");
    plugin.mMethodChannel.setMethodCallHandler(plugin);
//    registrar.platformViewRegistry().registerViewFactory("com.mob.adsdk/banner", new BannerAdViewFactory(plugin.mBinaryMessenger));
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    mMethodChannel.setMethodCallHandler(null);
  }


  @Override
  public void onAttachedToActivity(ActivityPluginBinding binding) {
    mActivity = new WeakReference<>(binding.getActivity());
    mApp.setCurrentActivity(mActivity.get());
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
    mActivity = null;
    mApp.setCurrentActivity(null);
  }

  @Override
  public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
    mActivity = new WeakReference<>(binding.getActivity());
    mApp.setCurrentActivity(mActivity.get());
  }

  @Override
  public void onDetachedFromActivity() {
    mActivity = null;
    mApp.setCurrentActivity(null);
  }

  @Override
  public void onMethodCall(@NonNull final MethodCall call, @NonNull final Result result) {
    switch (call.method) {
      case "initSdk":
        String appId = call.argument("id");
        String appKey = call.argument("key");
        ATSDK.init(getActivity(),appId,appKey);
        ATSDK.integrationChecking(getActivity());
        ATSDK.setNetworkLogDebug(false);
        result.success(true);
        break;
      case "setUserId":
        KawaAdPlugin.userId = call.argument("userId");
        result.success(true);
        break;
      case "loadRewardVideoAd":
        if (mRewardHandler == null){
          mRewardHandler = new RewardHandler(getActivity(),mBinaryMessenger,userId,(String) call.argument("unitId"));
        }
        mRewardHandler.loadReward((Integer)call.argument("_channelId"));
        break;
      case "showRewardVideoAd":
        mRewardHandler.show((Integer)call.argument("_channelId"),(String) call.argument("scene"));
        break;
      case "loadInterstitialAd":
        if (mIntersHandler == null){
          mIntersHandler = new IntersHandler(getActivity(),mBinaryMessenger,(String) call.argument("unitId"));
        }
        mIntersHandler.load((Integer)call.argument("_channelId"));
        break;
      case "showInterstitialAd":
        mIntersHandler.show((Integer)call.argument("_channelId"),(String) call.argument("scene"));
        break;
      case "loadSplashAd":
        if (mSplashHandler == null){
          mSplashHandler = new SplashHandler(getActivity(),mBinaryMessenger,(String) call.argument("unitId"));
        }
        mSplashHandler.load((Integer)call.argument("_channelId"));
        break;
      case "showSplashAd":
        mSplashHandler.show((Integer)call.argument("_channelId"),(String) call.argument("scene"));
        break;
      default:
        result.notImplemented();
        break;
    }
  }
}
